﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Resources;

namespace BoxDropbox
{
    public partial class BoxDropbox : Form
    {
        public readonly ResourceManager languageResource;
 
        public BoxDropbox()
        {
            #region LanguageCheck
            string systemLanguage = System.Globalization.CultureInfo.CurrentUICulture.EnglishName;
            if (systemLanguage == System.Globalization.CultureInfo.GetCultureInfo("en-US").EnglishName)
            {
                // U.S. ENGLISH
                languageResource = Config.string_en.ResourceManager;               
            }
            else
            {
                // DEFAULT TO U.S. ENGLISH
                languageResource = Config.string_en.ResourceManager;  
            }
            #endregion
            InitializeComponent();
        }

        /// <summary>
        /// The Main Application's Load Event
        /// </summary>
        /// <param name="sender">Object sending this message</param>
        /// <param name="e">Event Arguments</param>
        private void BoxDropbox_Load(object sender, EventArgs e)
        {

            // Initialize File Path
            if (Properties.Settings.Default.ROOT_DIR != "")
            {
                fldr_Directory.SelectedPath = Properties.Settings.Default.ROOT_DIR;
            }
            else
            {
                fldr_Directory.SelectedPath = Environment.GetEnvironmentVariable("USERPROFILE") + "\\Box Dropbox";
            }

            txt_Directory.Text = fldr_Directory.SelectedPath;
            DirectoryFetcher.Instance.root = fldr_Directory.SelectedPath;

            Notify_BDB.BalloonTipText = "Box Dropbox will continue to run in your tray!";
            Notify_BDB.BalloonTipTitle = "Box Dropbox";

            // Display the balloon (in ms)
            Notify_BDB.ShowBalloonTip(5000);

            // Have the user log in
            if (! BoxFetcher.Instance.startAuth())
            {
                Console.WriteLine("Error with authenticating. Application will shut down");
                Application.Exit();
            }


            // Change the WindowState to Minimized
            WindowState = FormWindowState.Minimized;
        }

        /// <summary>
        /// The Main Application's Resize Event
        /// </summary>
        /// <param name="sender">Object sending this message</param>
        /// <param name="e">Event Arguments</param>
        private void BoxDropbox_Resize(object sender, EventArgs e)
        {
            // Show the form in System Tray when it is Minimized
            if (FormWindowState.Minimized == WindowState)
            {
                Hide();
            }
        }

        /// <summary>
        /// Notify Icon's Double-Click Event
        /// </summary>
        /// <param name="sender">Object sending this message</param>
        /// <param name="e">Event Arguments</param>
        private void Notify_BDB_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            // Return the Application to its normal state
            WindowState = FormWindowState.Normal;
            Show();
        }

        #region Tray Context Menu Events
        /// <summary>
        /// Notify Icon's Context Menu Quit Click Event
        /// </summary>
        /// <param name="sender">Object sending this message</param>
        /// <param name="e">Event Arguments</param>
        private void quitBoxDropboxToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// Notify Icon's Context Menu Settings Click Event
        /// </summary>
        /// <param name="sender">Object sending this message</param>
        /// <param name="e">Event Arguments</param>
        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Normal;
            Show();
        }

        /// <summary>
        /// Notify Icon's Context Menu Poll Remote Click Event
        /// </summary>
        /// <param name="sender">Object sending this message</param>
        /// <param name="e">Event Arguments</param>
        private void pollRemoteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Poll for files on the Root folder
            BoxFetcher.Instance.pollForFiles(-1,Properties.Settings.Default.ROOT_DIR);
        }

        /// <summary>
        /// Notify Icon's Context Menu Poll Local Click Event
        /// </summary>
        /// <param name="sender">Object sending this message</param>
        /// <param name="e">Event Arguments</param>
        private void pollLocalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Poll local files
            DirectoryFetcher.Instance.PollForFiles();
        }

        
        private void addToRemoteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Console.WriteLine(BoxFetcher.Instance.getFolderFromPath("RIT\\Mobile Development"));
        }
        #endregion


        #region Settings Menu Events

        /// <summary>
        /// Settings Menu Directory Button Event
        /// </summary>
        /// <param name="sender">Object sending this message</param>
        /// <param name="e">Event Arguments</param>
        private void btn_Directory_Click(object sender, EventArgs e)
        {
            // Show the dialog
            DialogResult result = fldr_Directory.ShowDialog();

            if (result == DialogResult.OK)
            {
                txt_Directory.Text = fldr_Directory.SelectedPath;
                Properties.Settings.Default.ROOT_DIR = fldr_Directory.SelectedPath;
                Properties.Settings.Default.Save();
            }
        }

        /// <summary>
        /// Settings Menu Cancel Button Event
        /// </summary>
        /// <param name="sender">Object sending this message</param>
        /// <param name="e">Event Arguments</param>
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
            Hide();
        }

        /// <summary>
        /// Settings Menu OK Button Event
        /// </summary>
        /// <param name="sender">Object sending this message</param>
        /// <param name="e">Event Arguments</param>
        private void btn_OK_Click(object sender, EventArgs e)
        {
            // Store/Set all variables - CHANGE THIS
            string filePath = txt_Directory.Text;
            DirectoryFetcher.Instance.root = filePath;
            int interval = Convert.ToInt32(cbo_Interval.Text);

#if DEBUG_LOG
            Console.WriteLine("Saving filePath: " + filePath);
            Console.WriteLine("Saving interval: " + interval);
#endif

            WindowState = FormWindowState.Minimized;
            Hide();
        }

        #endregion

        /// <summary>
        /// Event to handle Auth Complete click - finishes authentication.
        /// </summary>
        /// <param name="sender">Object sending this message</param>
        /// <param name="e">Event Arguments</param>
        private void authCompleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BoxFetcher.Instance.finishAuth();
        }

        /// <summary>
        /// Event to handle DEBUG menu's clear settings context menu.
        /// </summary>
        /// <param name="sender">Object sending this message</param>
        /// <param name="e">Event Arguments</param>
        private void clearSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Reset();
        }

    }
}
